<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * App\VipInfo
 *
 * @property int $id
 * @property float $price 价格
 * @property string|null $type 会员类型 num：数量会员 time：时间会员
 * @property int $num 次数,会员类型为num时使用
 * @property string|null $end_time 到期时间,会员类型为time时使用
 * @property string|null $deleted_at
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\VipInfo newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\VipInfo newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\VipInfo query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\VipInfo whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\VipInfo whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\VipInfo whereEndTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\VipInfo whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\VipInfo whereNum($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\VipInfo wherePrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\VipInfo whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\VipInfo whereUpdatedAt($value)
 * @mixin \Eloquent
 * @property int $user_id 用户id
 * @method static \Illuminate\Database\Eloquent\Builder|\App\VipInfo whereUserId($value)
 * @property int $use_num 使用次数
 * @method static \Illuminate\Database\Eloquent\Builder|\App\VipInfo whereUseNum($value)
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Query\Builder|\App\VipInfo onlyTrashed()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Query\Builder|\App\VipInfo withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\VipInfo withoutTrashed()
 */
class VipInfo extends Model
{
    use SoftDeletes;
    //检查用户是否会员
    public static function getUserVipInfo($user_id = 0){
        if(empty($user_id))
            return [];

        $vipInfos = self::whereUserId($user_id)->whereColumn('num','>','use_num')->orWhere('end_time','>',date('Y-m-d H:i:s',time()))->whereUserId($user_id)->orderByDesc('num')->get();
        if(count($vipInfos) > 0){
            foreach ($vipInfos as $vipInfo) {
                $vipInfo->type_name = self::getVipTypeName($vipInfo->type);
            }
        }

        return $vipInfos;
    }

    public static function getVipTypeName($type = ''){
        if(empty($type))
            return '';

        switch ($type){
            case 'num':
                return '次数会员';
                break;
            case 'time':
                return '年费会员';
                break;
            default:
                return $type;
        }
    }
}
